﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FJASPX.Framwork.Constant;
using System.Web.Security;
using FJASPX.Framwork.Model;

namespace FJASPX.Framwork.UI
{
    public abstract class PageBase : System.Web.UI.Page
    {
        /// <summary>
        /// 页面权限
        /// </summary>
        public abstract string PowerCode { get; }
         
        /// <summary>
        /// 是否有权限
        /// </summary>
        public bool HavePower
        {
            get
            {
                if (ViewState[Strings.HAVE_POWER] == null)
                {
                    return false;
                }
                return true;
            }
            set
            {
                ViewState[Strings.HAVE_POWER] = value;
            }
        }

        
        /// <summary>
        /// 每页记录数
        /// </summary>
        protected int _pageSize = 999999999;

        /// <summary>
        /// 页码
        /// </summary>
        protected int _pageIndex;

        /// <summary>
        /// 总页数
        /// </summary>
        protected long _totalCount;
        /// <summary>
        /// 每页记录数
        /// </summary>
        public int PageSize
        {
            get { return this._pageSize; }
            set { this._pageSize = value; }
        }

        /// <summary>
        /// 页码
        /// </summary>
        public int PageIndex
        {
            get { return this._pageIndex; }
            set { this._pageIndex = value; }
        }
        /// <summary>
        /// 总页数
        /// </summary>
        public long TotalCount
        {
            get { return this._totalCount; }
            set { this._totalCount = value; }
        }

        /// <summary>
        /// 页面初始化时
        /// </summary>
        protected virtual bool InitPage()
        {
            return true;
        }

        /// <summary>
        /// 当页面第一次被加载时
        /// </summary>
        protected virtual bool FirstCallRead()
        {
            return true;
        }

        /// <summary>
        /// 页面每一次被加载时
        /// </summary>
        protected virtual bool CallRead()
        {
            return true;
        }
        /// <summary>
        /// 当前用户
        /// </summary>
        private MembershipUser _User;

        protected MembershipUser CurrentUser
        {
            get
            {
                if (this._User == null)
                {
                    this._User = Membership.GetUser(User.Identity.Name);
                }
                return this._User;
            }
        }


        /// <summary>
        /// 检测权限
        /// </summary>
        /// <returns>是否具有权限</returns>
        protected virtual bool CheckPower()
        {
            return false;
        }

        #region Events事件

        /// <summary>
        /// 页面初始化
        /// </summary>
        protected override void OnLoad(EventArgs e)
        {
            if (this.InitPage())
            {
                if (!this.Page.IsPostBack)
                {
                    if (!this.FirstCallRead())
                    {
                        base.OnLoad(e); return;
                    }
                }
                if (!this.CallRead())
                {
                    base.OnLoad(e); return;
                }
            }
            base.OnLoad(e);
        }

        #endregion
    }
}
